import type { FormSchemaGetter } from '#/adapter/form';
import type { VxeGridProps } from '#/adapter/vxe-table';

import { DictEnum } from '@vben/constants';
import { getPopupContainer } from '@vben/utils';

import { getDictOptions } from '#/utils/dict';
import { renderDict } from '#/utils/render';

export const querySchema: FormSchemaGetter = () => [
  {
    component: 'Input',
    fieldName: 'productName',
    label: '产品名称',
    colProps: { span: 6 },
  },
  {
    component: 'Select',
    colProps: { span: 6 },
    componentProps: {
      options: getDictOptions(DictEnum.IOT_PROTOCOL_TYPE),
    },
    fieldName: 'protocol',
    label: '协议类型',
  },
  {
    component: 'Select',
    colProps: { span: 6 },
    componentProps: {
      options: getDictOptions(DictEnum.IOT_DATA_TYPE),
    },
    fieldName: 'dataType',
    label: '数据格式',
  },
  {
    component: 'Select',
    fieldName: 'connectType',
    label: '连网方式',
    colProps: { span: 6 },
    componentProps: {
      options: getDictOptions(DictEnum.IOT_CONNECT_TYPE),
    },
  },
];

export const columns: VxeGridProps['columns'] = [
  {
    title: '产品名称',
    field: 'productName',
  },
  {
    title: '产品编码',
    field: 'productKey',
  },
  {
    title: '协议类型',
    field: 'protocol',
    slots: {
      default: ({ row }) => {
        return renderDict(`${row.protocol}`, DictEnum.IOT_PROTOCOL_TYPE);
      },
    },
  },
  {
    title: '数据格式',
    field: 'dataType',
    slots: {
      default: ({ row }) => {
        return renderDict(`${row.dataType}`, DictEnum.IOT_DATA_TYPE);
      },
    },
  },

  {
    title: '安全类型',
    field: 'securityType',
    slots: {
      default: ({ row }) => {
        return renderDict(`${row.securityType}`, DictEnum.IOT_SECURITY_TYPE);
      },
    },
  },
  {
    title: '连网方式',
    field: 'connectType',
    slots: {
      default: ({ row }) => {
        return renderDict(`${row.connectType}`, DictEnum.IOT_CONNECT_TYPE);
      },
    },
  },
  {
    title: '创建时间',
    field: 'createTime',
  },
  {
    field: 'action',
    fixed: 'right',
    slots: { default: 'action' },
    title: '操作',
    resizable: false,
    width: 'auto',
  },
];

export const modalSchema: FormSchemaGetter = () => [
  {
    component: 'Input',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
    fieldName: 'id',
    label: '主键',
  },
  {
    component: 'Input',
    fieldName: 'productName',
    label: '产品名称',
    rules: 'required',
  },
  {
    component: 'Select',
    componentProps: {
      getPopupContainer,
      options: getDictOptions(DictEnum.IOT_PROTOCOL_TYPE),
    },
    fieldName: 'protocol',
    label: '协议类型',
    rules: 'required',
    dependencies: {
      disabled(values) {
        return !!values.id;
      },
      triggerFields: ['id'],
    },
  },
  {
    component: 'Select',
    componentProps: {
      getPopupContainer,
      options: getDictOptions(DictEnum.IOT_DATA_TYPE),
    },
    fieldName: 'dataType',
    label: '数据格式',
    rules: 'required',
    dependencies: {
      disabled(values) {
        return !!values.id;
      },
      triggerFields: ['id'],
    },
  },
  {
    component: 'Select',
    componentProps: {
      getPopupContainer,
      options: getDictOptions(DictEnum.IOT_SECURITY_TYPE),
    },
    fieldName: 'securityType',
    label: '安全类型',
    rules: 'required',
    dependencies: {
      disabled(values) {
        return !!values.id;
      },
      triggerFields: ['id'],
    },
  },
  {
    component: 'Select',
    componentProps: {
      getPopupContainer,
      options: getDictOptions(DictEnum.IOT_CONNECT_TYPE),
    },
    fieldName: 'connectType',
    label: '连网方式',
    rules: 'required',
  },
  {
    component: 'Textarea',
    fieldName: 'productDesc',
    label: '产品描述',

    componentProps: {
      maxlength: 100,
      showCount: true,
      placeholder: '请输入产品描述',
    },
  },
];
